<template>
  <div class="login-account">
    <el-form label-width="60px" :rules="rules" :model="account" ref="formRef">
      <el-form-item label="账号" prop="username">
        <el-input v-model="account.username" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="account.password" show-password />
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { saveInfo, setToken } from '@/utils/auth.js'
import { rules } from '../config/account-config'
import { login } from '@/api/user/index.js'
import { useRouter } from 'vue-router'
import {useUserStore} from '@/store/index.js'
const props = defineProps(['account'])

const router = useRouter()
const userStore=useUserStore()
const formRef = ref()

const loginAction = (isKeepPassword) => {
  formRef.value?.validate((valid) => {
    if (valid) {
      saveInfo('username', isKeepPassword ? props.account.username : null)
      saveInfo('password', isKeepPassword ? props.account.password : null)

      login(props.account).then(el => {
        if (el.code === 0) {
          setToken(el.token)
          userStore.setUserInfo()
          router.push('/home')
        }
      })
    }
  })
}

defineExpose({ loginAction })

</script>

<style scoped></style>
